<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="平台名称" prop="aiAgentPlatName">
        <el-input
          v-model="queryParams.aiAgentPlatName"
          placeholder="请输入平台名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台账号" prop="aiAgentPlatAccountName">
        <el-input
          v-model="queryParams.aiAgentPlatAccountName"
          placeholder="请输入平台账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['biz:aiAgentPlat:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:aiAgentPlat:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:aiAgentPlat:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['biz:aiAgentPlat:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="aiAgentPlatList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="系统编号" align="center" prop="aiAgentPlatId" /> -->
      <el-table-column label="平台名称" align="center" prop="aiAgentPlatName" />
      <el-table-column
        label="平台账号"
        align="center"
        prop="aiAgentPlatAccountName"
      />
      <!-- <el-table-column label="令牌" align="center" prop="aiAgentPlatAccessToken" /> -->
      <el-table-column
        label="空间名称"
        align="center"
        prop="aiAgentPlatWorkspaceName"
      />
      <!-- <el-table-column label="平台空间" align="center" prop="aiAgentPlatWorkspaceId" /> -->
      <!-- <el-table-column label="创建人" align="center" prop="createdBy" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="更新人" align="center" prop="updatedBy" /> -->
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatedTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:aiAgentPlat:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleCheckAiAgentPlatAccessTokenSta(scope.row)"
            v-hasPermi="['biz:aiAgentPlat:edit']"
            >验证</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleGetAiAgentList(scope.row)"
            v-hasPermi="['biz:aiAgentPlat:edit']"
            >获取智能体</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:aiAgentPlat:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改智能体平台对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="aiAgentPlatRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <!-- <el-form-item label="平台名称" prop="aiAgentPlatName">
          <el-input v-model="form.aiAgentPlatName" placeholder="请输入平台名称" />
        </el-form-item> -->

        <el-form-item label="平台名称" prop="aiAgentPlatName">
          <el-select
            v-model="form.aiAgentPlatName"
            placeholder="请选择平台名称"
          >
            <el-option label="取消" value=""></el-option>
            <el-option label="扣子" value="扣子"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="平台账号" prop="aiAgentPlatAccountName">
          <el-input
            v-model="form.aiAgentPlatAccountName"
            placeholder="请输入平台账号"
          />
        </el-form-item>
        <el-form-item label="访问令牌" prop="aiAgentPlatAccessToken">
          <el-input
            v-model="form.aiAgentPlatAccessToken"
            placeholder="请输入平台访问令牌"
          />
          <el-button
            type="primary"
            @click="
              handleGetAiAgentPlatWorkspaceIdList(form.aiAgentPlatAccessToken)
            "
            >获取空间列表</el-button
          >
        </el-form-item>

        <el-form-item label="空间列表">
          <el-select
            @change="handleAiAgentPlatWorkspaceChange"
            v-model="form.aiAgentPlatWorkspace"
            placeholder="请选择平台空间"
          >
            <el-option key="" label="取消" value=""></el-option>
            <el-option
              v-for="item in aiAgentPlatWorkspaceIdList"
              :key="item.id"
              :label="item.name"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="空间名称" prop="aiAgentPlatWorkspaceName">
          <el-input
            v-model="form.aiAgentPlatWorkspaceName"
            placeholder="请输入平台空间"
          />
        </el-form-item>
        <el-form-item label="空间号" prop="aiAgentPlatWorkspaceId">
          <el-input
            v-model="form.aiAgentPlatWorkspaceId"
            placeholder="请输入平台空间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AiAgentPlat">
import {
  listAiAgentPlat,
  getAiAgentPlat,
  delAiAgentPlat,
  addAiAgentPlat,
  updateAiAgentPlat,
  getAiAgentPlatWorkspaceIdList,
  checkAiAgentPlatAccessTokenSta,
} from "@/api/biz/aiAgentPlat";

const { proxy } = getCurrentInstance();

const aiAgentPlatList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  aiAgentPlatWorkspaceIdList: [],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    aiAgentPlatName: null,
    aiAgentPlatAccountName: null,
    createdTime: null,
    updatedTime: null,
  },
  rules: {
    aiAgentPlatName: [
      { required: true, message: "平台名称不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules, aiAgentPlatWorkspaceIdList } = toRefs(data);

/** 查询智能体平台列表 */
function getList() {
  loading.value = true;
  listAiAgentPlat(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      aiAgentPlatList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    aiAgentPlatId: null,
    aiAgentPlatName: null,
    aiAgentPlatAccountName: null,
    aiAgentPlatAccessToken: null,
    aiAgentPlatWorkspaceName: null,
    aiAgentPlatWorkspaceId: null,
    revision: null,
    createdBy: null,
    createdTime: null,
    updatedBy: null,
    updatedTime: null,
    dataSta: null,
  };
  aiAgentPlatWorkspaceIdList.value = [];
  proxy.resetForm("aiAgentPlatRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.aiAgentPlatId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加智能体平台";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  console.log(123123);
  reset();
  const _aiAgentPlatId = row.aiAgentPlatId || ids.value;
  getAiAgentPlat(_aiAgentPlatId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改智能体平台";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["aiAgentPlatRef"].validate((valid) => {
    if (valid) {
      if (form.value.aiAgentPlatId != null) {
        updateAiAgentPlat(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAiAgentPlat(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _aiAgentPlatIds = row.aiAgentPlatId || ids.value;
  proxy.$modal
    .confirm('是否确认删除智能体平台编号为"' + _aiAgentPlatIds + '"的数据项？')
    .then(function () {
      return delAiAgentPlat(_aiAgentPlatIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "biz/aiAgentPlat/export",
    {
      ...queryParams.value,
    },
    `aiAgentPlat_${new Date().getTime()}.xlsx`
  );
}

/** 验证token是否有效操作 */
function handleCheckAiAgentPlatAccessTokenSta(row) {
  console.log(row);
  const _aiAgentPlatId = row.aiAgentPlatId || ids.value;
  checkAiAgentPlatAccessTokenSta(_aiAgentPlatId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改智能体平台";
  });
}

function handleGetAiAgentPlatWorkspaceIdList(aiAgentPlatAccessToken) {
  getAiAgentPlatWorkspaceIdList(aiAgentPlatAccessToken).then((response) => {
    console.log(response);
    console.log(response.data);
    console.log(aiAgentPlatWorkspaceIdList);
    aiAgentPlatWorkspaceIdList.value = response.data;
    console.log(aiAgentPlatWorkspaceIdList);
    console.log("form");
    console.log(form);
  });
}
function handleAiAgentPlatWorkspaceChange(item) {
  console.log(121212);
  console.log(item);
  console.log(form);
  if (item) {
    // 从选中的对象中提取 id 和 name，分别赋值给表单
    data.form.aiAgentPlatWorkspaceId = item.id;
    data.form.aiAgentPlatWorkspaceName = item.name;
  } else {
    // 未选择时清空
    data.form.aiAgentPlatWorkspaceId = "";
    data.form.aiAgentPlatWorkspaceName = "";
  }
  console.log(form);
}
getList();
</script>
